Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer cached encryptionKey for encryption when possible #307

Merged
merged 2 commits into from
Nov 23, 2023

Conversation

mikesposito
Copy link
Member

Description

When persisting keyrings, EthKeyringController always prefers the use of the password over the cached encryption key. This means that we'll always derive the password (which is an heavy task), even if we already have the key that we need.

This PR changes the order of the check, putting the encryptionKey on top. Some changes were required on some tests after that as well.

Changes

  • FIXED: Use cached encryption key when available

References

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation for new or updated code as appropriate (note: this will usually be JSDoc)
  • I've highlighted breaking changes using the "BREAKING" category above as appropriate

@mikesposito mikesposito requested a review from a team as a code owner November 23, 2023 19:35
@@ -930,6 +930,7 @@ class KeyringController extends EventEmitter {

if (
this.password &&
(!this.#cacheEncryptionKey || !encryptionKey) &&
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ensures that we don't do the whole persistAllKeyrings operation for nothing, as the vault will not get updated if we reuse the cached encryption key

@mikesposito mikesposito changed the title Prefer encryptionKey for encryption when possible Prefer cached encryptionKey for encryption when possible Nov 23, 2023
},
});
await keyringController.createNewVaultAndKeychain(PASSWORD);
deleteEncryptionKeyAndSalt(keyringController);
Copy link
Member

@Gudahtt Gudahtt Nov 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see; this method is needed because this kinda isn't a real use-case. Or it is, but only as part of the initial unlock/vault creation, it's never a use case when calling persistAllKeyrings directly.

Makes sense. Seems like a bit of a code smell that this condition is possible only when this external method is called in a certain way internally, but that problem will go away soon hopefully when we merge this into core.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nicer way to test this would be to test it as part of createNewVaultAndKeychain (and the other methods that trigger this condition), but given that this is to be deleted soon I don't think it'd be worth the effort 😅


expect(initialVault).not.toBe(updatedVault);
expect(updatedVault).toBe(updatedVaultMock);
describe(`with cacheEncryptionKey = true and encryptionKey is unset`, () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Is there a test case for when cacheEncryptionKey = true and encryptionKey is set?

Copy link
Member Author

@mikesposito mikesposito Nov 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in fe45b61!

The test checks that the vault is not being updated

Gudahtt
Gudahtt previously approved these changes Nov 23, 2023
Copy link
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@mikesposito mikesposito merged commit ec3b12c into main Nov 23, 2023
17 checks passed
@mikesposito mikesposito deleted the fix/use-encryption-key branch November 23, 2023 20:11
mikesposito added a commit that referenced this pull request Nov 24, 2023
mikesposito added a commit that referenced this pull request Nov 29, 2023
* fix: prefer encryptionKey for encryption when possible

* refactor: add test case
mikesposito added a commit that referenced this pull request Nov 29, 2023
* Prefer cached `encryptionKey` for encryption when possible (#307)

* fix: prefer encryptionKey for encryption when possible

* refactor: add test case

* Use encryptor `isVaultUpdated` (#310)

* chore: update browser-passworder

* refactor: remove `updateVault` from `GenericEncryptor`
Gudahtt added a commit that referenced this pull request Nov 29, 2023
* origin/main:
  Use encryptor `isVaultUpdated` (#310)
  Bump @metamask/browser-passworder from 4.2.0 to 4.3.0 (#311)
  Prefer cached `encryptionKey` for encryption when possible (#307)
  Bump @metamask/obs-store from 8.1.0 to 9.0.0 (#306)
  Bump @metamask/eth-sig-util from 7.0.0 to 7.0.1 (#302)
Gudahtt added a commit that referenced this pull request Jan 9, 2024
* [15.x] Backport encryption improvements (#312)

* Prefer cached `encryptionKey` for encryption when possible (#307)

* fix: prefer encryptionKey for encryption when possible

* refactor: add test case

* Use encryptor `isVaultUpdated` (#310)

* chore: update browser-passworder

* refactor: remove `updateVault` from `GenericEncryptor`

* 15.1.0 (#313)

* 15.1.0

* edit changelog entries

* rephrase changelog entry

---------

Co-authored-by: github-actions <github-actions@github.com>
Co-authored-by: Michele Esposito <michele@esposito.codes>

---------

Co-authored-by: Michele Esposito <34438276+mikesposito@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions <github-actions@github.com>
Co-authored-by: Michele Esposito <michele@esposito.codes>
Co-authored-by: legobeat <109787230+legobeat@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants